/**
 * Created by gaomeng on 2017/1/15.
 */
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;

import jdk.nashorn.api.scripting.JSObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.jsoup.nodes.*;
import org.json.JSONObject;

public class Runtime {

    static Connection conn=null;
    public static String getJson(String url) {
        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpGet req = new HttpGet(url);
        //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
                    .setConnectionRequestTimeout(1000).setSocketTimeout(3000).build();

            req.setConfig(requestConfig);

            req.addHeader("Accept-Encoding", "gzip, deflate, sdch");
            req.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
            req.addHeader("Connection", "keep-alive");
            req.addHeader("Content-Type", "text/html;charset=UTF-8");
            HttpResponse rep = httpClient.execute(req);
            HttpEntity repEntity = rep.getEntity();
            return EntityUtils.toString(repEntity);
        }
        catch (ClientProtocolException e) {e.printStackTrace();}
        catch (IOException e) {e.printStackTrace();}
        return null;
    }

    //解析抓包获取的json文件
    public static void Datas(String jsonUrl) {
        try {
            String content = getJson(jsonUrl + 1);

            JSONObject jsonObject = new JSONObject(content);

            JSONObject object1 = jsonObject.getJSONObject("datas");

            if (object1.toString().equals("{}")) {
                return;
            }
            JSONObject obj2 = object1.getJSONObject("pages");

            int count = obj2.getInt("counts");
            int size = obj2.getInt("size");
            //获取总页数
            int pages = count / size;
            for (int j = 1; j <= page; j++) {
                String content1 = getJson(jsonUrl + j);
                JSONObject jsonObject1 = new JSONObject(content1);
                JSONObject object2 = jsonObject1.getJSONObject("datas");
                JSONObject object3 = object2.getJSONObject("archives");

                //获取数据关键步骤
                for (int i = 0; i < object3.length(); i++) {
                    JSONObject object = object3.getJSONObject(String.valueOf(i));
                    String author = object.getString("author");
                    String title = object.getString("title");
                    int favorites = object.getInt("favorite");

                    //保存URL下载视频时使用
                    String url = "http://www.ibilibili.com/video/av" + "/";

                    Bilibili bilibili=new Bilibili();
                    bilibili.setAuthor(author);
                    bilibili.setFavorite(favorite);
                    bilibili.setTitle(title);
                    bilibili.setUrl(url);
                    //将数据存入数据库
                    JDBC.insert(JDBC.getConnection(),bilibili);
                }
            }

        }
        catch (Exception e) {e.printStackTrace();}
    }


